(function () {
  main();

  function main() {
    if (window.AXURE_LOTTIE) return;
    window.AXURE_LOTTIE = {};
    $.getScript('https://ax.minicg.com/axloader.js', ()=>{
      yepnope({
        load: [
          'https://ax.minicg.com/axlib.js',
          'https://ax.minicg.com/lottie.js',
          'https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js'
        ],
        complete: inited
      });
    });
  }

  function inited() {
    // ExtendindAxure();

    let targets = $('div[data-label="axlib-lottie"]');
    $.each(targets, (i, n) => {
      let box = $(n);
      let bid = box.attr('id');
      let rep = box.parent().parent();
      let rid = rep.attr('id');
      let repData = axlib.repeater.getData(rid);
      let data = axlib.repeater.format(repData, 3, { single: true  });

      let json = data['动画数据'];
      let autoplay = data['自动开始'].toLowerCase()=='true'?true:false;
      let loop  = data['循环播放'].toLowerCase()=='true'?true:false;
      let ghost = data['鼠标穿透'].toLowerCase()=='true'?'none':'auto';
      let blend = data['混合模式'];
      let filter = data['效果滤镜'];

      let container = box.children('div').eq(0);
      let cid = container.attr('id');
      box.addClass('axlib-lottie');
      box.children('.text').hide();
      
      let url = `http://fake.url/data_${bid}.json`;
      Mock.mock(url, 'get', opt => {
        return JSON.parse(json);
      });

      window.AXURE_LOTTIE[bid] = bodymovin.loadAnimation({
        container: document.getElementById(cid),
        path: url,
        renderer: 'svg',
        loop: loop,
        autoplay: autoplay
      });
      
      box.css({
        'mix-blend-mode': blend,
        'pointer-events': ghost,
        'filter': filter
      });

      // $(document).on('LOTTIE_UPDATED', ()=>{
      //   console.log(arguments);
      // });
    });
  }

  // function ExtendindAxure() {
  //   if (!$axure.ex) $axure.ex = { fn:{} };
  //   $axure.internal(function ($ax) {
  //     let refreshPeater = $ax.repeater.refreshRepeater;
  //     $ax.repeater.refreshRepeater = function() {
  //       $(document).trigger('LOTTIE_UPDATED', [arguments[0]]);
  //       refreshPeater(...arguments);
  //     }
  //   });
  // }
}());